//
//  SLDebugSuspendView.m
//  SLLoginModule_Example
//
//  Created by shunlian on 2019/11/7.
//  Copyright © 2019 童玉龙. All rights reserved.
//

#import "SLDebugSuspendView.h"
#import "SLNavigationController.h"
#import "LHTestConfigInfoVC.h"

@interface SLDebugSuspendView ()

@property (nonatomic, strong) UIImageView *backgroundImageView;

@end

@implementation SLDebugSuspendView

- (instancetype)initWithFrame:(CGRect)frame
{
    CGFloat width = 45;
    self = [super initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width - width, 200, width, width)];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.layer.cornerRadius = width/2.f;
        self.layer.shadowColor = [UIColor colorWithWhite:0 alpha:1].CGColor;
        self.layer.shadowOffset = CGSizeMake(0,0);
        self.layer.shadowOpacity = 0.2;
        self.layer.shadowRadius = 3;
        [self addSubview:self.backgroundImageView];
        [self configGesture];
        [self fixedDebugView];
    }
    return self;
}

- (void)configGesture {
    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(onPanGestureAction:)];
    [self addGestureRecognizer:panGesture];
    
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTapGestureAction:)];
    [self addGestureRecognizer:tapGesture];
}

- (void)onTapGestureAction:(UITapGestureRecognizer *)tap {

    LHTestConfigInfoVC *vc = [[LHTestConfigInfoVC alloc] init];
    
    SLNavigationController *nav = [[SLNavigationController alloc] initWithRootViewController:vc];
    
    [CurrentVC presentViewController:nav animated:YES completion:0];
    //弹出debug弹框
}

- (void)onPanGestureAction:(UIPanGestureRecognizer *)pan {
    if (pan.state == UIGestureRecognizerStateBegan) {
        CGFloat width = 45;
        CGRect frame = self.frame;
        if (frame.origin.x < 0) {
            frame.origin.x = 0;
        }
        frame.size.width = width;
        self.frame = frame;
        self.backgroundImageView.frame = CGRectInset(self.bounds, 6, 6);
    }else if (pan.state == UIGestureRecognizerStateEnded) {
        CGRect frame = self.frame;
        if (self.center.x > [UIScreen mainScreen].bounds.size.width/2.f) {
            frame.origin.x = [UIScreen mainScreen].bounds.size.width - frame.size.width;
        }else {
            frame.origin.x = 0;
        }
        [UIView animateWithDuration:.25 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            self.frame = frame;
        } completion:^(BOOL finished) {
            [self fixedDebugView];
        }];
    }else{
        CGPoint translation = [pan translationInView:self];
        self.center = CGPointMake(self.center.x + translation.x, self.center.y + translation.y);
        [pan setTranslation:CGPointZero inView:self];
    }
}

- (void)fixedDebugView {
    CGRect viewFrame = self.frame;
    if (viewFrame.origin.x == 0) {
        viewFrame.origin.x = -15;
        viewFrame.size.width += 15;
        self.frame = viewFrame;
        CGRect backgroundImageViewFrame = self.backgroundImageView.frame;
        backgroundImageViewFrame.origin.x += 15;
        self.backgroundImageView.frame = backgroundImageViewFrame;
    }else {
        viewFrame.size.width += 15;
        self.frame = viewFrame;
    }
}

#pragma mark - Getters

- (UIImageView *)backgroundImageView {
    if (!_backgroundImageView) {
        _backgroundImageView = [[UIImageView alloc] initWithFrame:CGRectInset(self.bounds, 6, 6)];
        _backgroundImageView.image = [UIImage imageNamed:@"img_login_logo"];
        _backgroundImageView.layer.cornerRadius = _backgroundImageView.frame.size.height/2.f;
        _backgroundImageView.clipsToBounds = YES;
    }
    return _backgroundImageView;
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/


#pragma mark - SLDebugSuspendView 状态存储


//第一次加载
+(void)initializeView{
#if DEBUG
    //先默认显示
    [[NSUserDefaults standardUserDefaults] setObject:@(1) forKey:@"DebugSuspendViewStatus"];
    
    NSNumber *status = (NSNumber *)[[NSUserDefaults standardUserDefaults] objectForKey:@"DebugSuspendViewStatus"];
    if (!status || [status boolValue]) {//第一次 或者 状态为显示
        [SLDebugSuspendView changeDebugSuspendView];
    }
#endif
}

+(BOOL)changeDebugSuspendView{
    
    SLDebugSuspendView *suspendView = nil;
    for (UIView *view in [UIApplication sharedApplication].keyWindow.subviews) {
        if ([view isKindOfClass:[SLDebugSuspendView class]]) {
            suspendView = (SLDebugSuspendView *)view;
            break;
        }
    }
    if (suspendView) {
        [suspendView removeFromSuperview];
        [SLDebugSuspendView setSuspendViewStatus:NO];
        return NO;
    }else {
        suspendView = [[SLDebugSuspendView alloc] init];
        [[UIApplication sharedApplication].keyWindow addSubview:suspendView];
        [SLDebugSuspendView setSuspendViewStatus:YES];
        return YES;
    }
}

+(void)setSuspendViewStatus:(BOOL)isShow{
    
    [[NSUserDefaults standardUserDefaults] setObject:@(isShow) forKey:@"DebugSuspendViewStatus"];
}

+(BOOL)getStatus{
    NSNumber *status = (NSNumber *)[[NSUserDefaults standardUserDefaults] objectForKey:@"DebugSuspendViewStatus"];
    return [status boolValue];
}
@end
